package com.ekingstar.framework.action;

import java.util.Date;

import org.apache.struts2.config.Action;
import org.springframework.security.concurrent.SessionInformation;
import org.springframework.security.concurrent.SessionRegistry;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.app.ApplicationParameter;
import com.ekingstar.framework.domain.OnlineUser;
import com.ekingstar.framework.domain.SystemLog;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.IOnlineUserService;
import com.ekingstar.framework.service.ISystemLogService;
import com.ekingstar.framework.util.Page;

/**
 * 
 * @author GQ
 *
 */

@Action(name="onlineusers",namespace="/syslog")
public class ListOnlineUserAction extends ActionSupport {

	private Page page;
	private int pageNo = 1;
	private int pageSize = ApplicationParameter.INSTANCE.getPageSize();
	
	private IOnlineUserService onlineUserService;
	private OnlineUser onlineUser;
	private SessionRegistry sessionRegistry;
	private ISystemLogService systemLogService;
	public void setSystemLogService(ISystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}

	public void setSessionRegistry(SessionRegistry sessionRegistry) {
		this.sessionRegistry = sessionRegistry;
	}

	public String execute(){
		page = onlineUserService.getOnlineUserPage(onlineUser, pageNo, pageSize);	
		return SUCCESS;
	}
	
	public String kickUser(){
		try {
			String userName = this.onlineUser.getUserName();
			SessionInformation[] sis = sessionRegistry.getAllSessions(userName, false);
			for (int i =0;sis != null && i < sis.length;i++){
				sis[i].expireNow();
			}
			User user = new User();
			//
			user = UserUtil.getCurrentUser();
			String userid = user.getId();
			String ip = onlineUserService.getIpByCurrentUser(user);
			SystemLog systemLog = new SystemLog();
			systemLog.setUserId(userid);
			systemLog.setTime(new Date());
			systemLog.setType(SystemLog.LogType.kick_user.name());
			systemLog.setIp(ip);
			systemLog.setModelName("系统管理");
			systemLog.setDescription("踢出用户(用户帐号:" + userName + ")");
			systemLogService.addLog(systemLog);
			//
			user.setName(userName);
			onlineUserService.deleteUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		page = onlineUserService.getOnlineUserPage(onlineUser, pageNo, pageSize);	
		return SUCCESS;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public IOnlineUserService getOnlineUserService() {
		return onlineUserService;
	}

	public void setOnlineUserService(IOnlineUserService onlineUserService) {
		this.onlineUserService = onlineUserService;
	}

	public OnlineUser getOnlineUser() {
		return onlineUser;
	}

	public void setOnlineUser(OnlineUser onlineUser) {
		this.onlineUser = onlineUser;
	}
}
